package org.jeecgframework.core.util.wordseg;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;

import com.chenlb.mmseg4j.ComplexSeg;
import com.chenlb.mmseg4j.Dictionary;
import com.chenlb.mmseg4j.MMSeg;
import com.chenlb.mmseg4j.Seg;
import com.chenlb.mmseg4j.Word;

/**
 * 
 * <p>
 * 标题: MMseg4j分词工具类
 * </p>
 * <p>
 * 描述: // TODO
 * </p>
 * 
 * @author
 * @version // TODO
 * @since Apr 29, 2014 4:58:40 PM
 */
public class MmSegment {
	
	
	static List<String> stoplist = null;
	/**
	 * 
	 * <p>
	 * 方法简要描述: 进行分词
	 * </p>
	 * <p>
	 * 方法详细描述: // TODO
	 * </p>
	 * 
	 * @author 
	 * @version 
	 * @see
	 * @param filepath	文件路径
	 * @return
	 * @throws Exception
	 */
	public static List<String> segment(File file) throws Exception{		
		Dictionary dic = Dictionary.getInstance();//建立词典实例，与比较老的版本中不相同。不能直接new。
		Seg seg = null; 
		seg = new ComplexSeg(dic);		
		String txt = getContent(file);
		MMSeg mmSeg = new MMSeg(new StringReader(txt), seg); 
		Word word = null;
		List<String> list = new ArrayList<String>();
		while((word = mmSeg.next())!=null) {
			if(filter(word.getString()))
				list.add(word.getString());
		}
		return list;
	}
	/**
	 * 
	 * <p>
	 * 方法简要描述: 对String进行分词
	 * </p>
	 * <p>
	 * 方法详细描述: // TODO
	 * </p>
	 * 
	 * @author 
	 * @version 
	 * @see
	 * @param txt
	 * @return
	 * @throws Exception
	 */
	public static List<String> segment(String txt) throws Exception{	
		if(txt == null) return null;
		Dictionary dic = Dictionary.getInstance();//建立词典实例，与比较老的版本中不相同。不能直接new。
		Seg seg = null; 
		seg = new ComplexSeg(dic);		
		MMSeg mmSeg = new MMSeg(new StringReader(txt), seg); 
		Word word = null;
		List<String> list = new ArrayList<String>();
		while((word = mmSeg.next())!=null) {
			if(filter(word.getString()) && filterStop(word.getString()))
				list.add(word.getString());
		}
		return list;
	}
	/**
	 * 
	 * <p>
	 * 方法简要描述: 去除停住词(配置在stop-word中)
	 * </p>
	 * <p>
	 * 方法详细描述: // TODO
	 * </p>
	 * 
	 * @author 
	 * @version 
	 * @see
	 * @param word
	 * @return
	 */
	static boolean filterStop(String word){
		if(stoplist == null){
			stoplist = new ArrayList<String>();
			String stopPath = MmSegment.class.getResource("").getPath();
			stopPath += "stop.txt";
			String stop = getContent(new File(stopPath));
			for(int i=0;i<stop.length();i++){
				stoplist.add(stop.charAt(i)+"");
			}
				
		}
		return !stoplist.contains(word);
	}
	
	/**
	 * 
	 * <p>
	 * 方法简要描述: 过滤点一些英文和数字
	 * </p>
	 * <p>
	 * 方法详细描述: // TODO
	 * </p>
	 * 
	 * @author 
	 * @version 
	 * @see
	 * @param str
	 * @return
	 */
	static boolean filter(String str){
		String f = str.substring(0,1);
		if((f.compareTo("0") >= 0 && f.compareTo("9") <=0)
				|| (f.compareTo("A") >= 0 && f.compareTo("Z") <=0)
				|| (f.compareTo("a") >= 0 && f.compareTo("z") <=0)
				)		
			return false;
		return true;
	}
	
	
	public static String getContent(File file) {
		StringBuffer content = new StringBuffer("");
		InputStream is = null;
		BufferedInputStream bis = null;
		BufferedReader bReader = null;
		String line = null;
		try {
			is = new FileInputStream(file);
			bis = new BufferedInputStream(is);
			String charset = "utf-8";			
			bReader = new BufferedReader(new InputStreamReader(bis, charset));
			line = bReader.readLine();
			while (line != null) {
				content.append(line);
				line = bReader.readLine();
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new RuntimeException("");
		}finally{			
			try {
				if(bReader != null)
					bReader.close();
				if(bis != null)
					bis.close();
				if(is != null)
					is.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}		
		}	
		return content.toString();
	}
	
	/**
	 * 
	 * <p>
	 * 方法简要描述: 将文件从一个文件夹复制到另一个
	 * </p>
	 * <p>
	 * 方法详细描述: // TODO
	 * </p>
	 * 
	 * @author 
	 * @version 
	 * @see
	 * @param sFile
	 * @param outputFile
	 * @throws FileNotFoundException
	 */
	public static void copyFile(File sFile,String outputFile) throws FileNotFoundException{
		File tFile = new File(outputFile);
		FileInputStream fis = new FileInputStream(sFile);
		FileOutputStream fos = new FileOutputStream(tFile);
		InputStreamReader in = null;
		OutputStreamWriter out = null;
		int temp = 0;  
		char[] buf = new char[2048];    

        try {  
        	in = new InputStreamReader(fis,"gbk");
        	out = new OutputStreamWriter(fos,"gbk");
        	while((temp = in.read(buf))!=-1){   
                out.write(buf, 0, temp);   
            }   
        } catch (IOException e) {  
            e.printStackTrace();  
        } finally{
            try {
            	in.close();
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
        } 
	}
	
	public static void main(String[] args) {
		String s = "我们都是一家子，哈哈";
		System.out.println(filterStop(s)+"    "+stoplist.size());
	}
	

}